package ViewsTUI;

import Controllers.*;
import Models.*;
import byggemarked.Console;
import java.util.ArrayList;

/**
 * @author Erik & Thomas
 */
public class EmployeeView
{
	private EmployeeCTR controller;

    public EmployeeView()
	{
		this.controller = new EmployeeCTR();
		ArrayList<String> options = new ArrayList<String>();
		options.add("Tilføj ansat");
		options.add("Find ansat");
		options.add("Hent ansat");
		options.add("Redigér ansat");
		options.add("Slet ansat");

		boolean isTimeToQuit = false;
		while(!isTimeToQuit)
		{
			Console.printMenu(true, "Ansatte", options);
			int choice = Console.readChoice(0, 4);
			switch(choice)
			{
				case 1: addEmployee();		break;
				case 2: findEmployee();		break;
				case 3: getEmployee();		break;
				case 4: editEmployee();		break;
				case 5: removeEmployee();	break;
				case 0: isTimeToQuit = true;
			}
		}
    }

	private void addEmployee()
	{
		Console.print("Indtast navn:");
		String name = Console.readString();

		Console.print("Indtast lokation for den ansatte (1 = Trælasthandelen, 2 = Byggemarkedet):");
        int locationInt = Console.readInt(true);
        Location location;
        switch(locationInt)
        {
			case 1:
				location = Location.LumberYard;
				break;
			case 2:
				location = Location.HardwareStore;
				break;
			default:
				location = null;
		}

		this.controller.addEmployee(name, location);
	}

	private void findEmployee(boolean standAlone)
	{
		Console.print("For at søge efter en ansat skal du indtaste mindst et af sørekriterierne.");
		Console.print("Indtast navn:");
		String name = Console.readString(true);

		Console.print("Indtast lokation (1 = Trælasthandelen, 2 = Byggemarkedet)");
		int locationInt = Console.readInt(true);
		Location location;
		if(locationInt == 1)
		{
			location = Location.LumberYard;
		}
		else if(locationInt == 2)
		{
			location = Location.HardwareStore;
		}
		else
		{
			location = null;
		}

		ArrayList<Employee> employees = this.controller.findEmployee(name, location);
		if(employees.size() > 0)
		{
			this.showList(employees);
			if(standAlone)
			{
				Console.print("Indtast den ønskede ansattes ID, som står ude til venstre i listen.");
				int id = Console.readInt();
				Employee employee = this.controller.getEmployee(id);
				if(employee != null)
				{
					this.showInfo(employee);
				}
				else
				{
					Console.print("Der kunne ikke findes nogen ansatte med det valgte ID.");
				}
			}
		}
	}

	private void findEmployee()
	{
		this.findEmployee(true);
	}

	private void getEmployee()
	{
		Console.print("Indtast den ansattes ID.");
		int employeeInt = Console.readInt();
		Employee employee = this.controller.getEmployee(employeeInt);
		if(employee != null)
		{
			showInfo(employee);
		}
		else
		{
			Console.print("Der kunne ikke findes nogen ansat med det valgte ID.");
		}
	}

	private void editEmployee()
	{
		Console.print("For at redigere en ansat, skal den først findes.");
		Employee employee = null;
		boolean hasValidEmployee = false;
		while(!hasValidEmployee)
		{
			try
			{
				employee = searchProcedure();
				if(employee != null)
				{
					hasValidEmployee = true;
				}
				else
				{
					return;
				}
			}
			catch(Exception e)
			{ }
		}
	}

	private void removeEmployee()
	{
		Console.print("For at slette en ansatte, skal den først findes.");
		Employee employee = null;
		boolean hasValidEmployee = false;
		while(!hasValidEmployee)
		{
			try
			{
				employee = searchProcedure();
				if(employee != null)
				{
					hasValidEmployee = true;
				}
				else
				{
					return;
				}
			}
			catch(Exception e)
			{ }
		}
		String name = employee.getName();
		this.controller.removeEmployee(employee);
		Console.print(name+" er nu slettet fra ansattedatabasen.");
	}

	private Employee searchProcedure() throws Exception
	{
		Console.print("Hvordan vil du finde den ansatte?");
		ArrayList<String> options = new ArrayList<String>();
		options.add("ID");
		options.add("Søgning");
		options.add("Liste");
		Console.printMenu(false, null, options);

		int choice = Console.readChoice(0, 3);
		int id = 0;
		boolean doSomething = true;
		switch(choice)
		{
			case 1:
				Console.print("Indtast den ansattes ID.");
				id = Console.readInt();
				break;
			case 2:
				Console.print("Indtast den ønskede ansattes ID, som står til venstre i listen af søgeresultater.");
				this.findEmployee();
				id = Console.readInt();
				break;
			case 3:
				Console.print("Indtast den ønskede ansattes ID, som står til venstre i listen.");
				this.showList(this.controller.getAllEmployee());
				id = Console.readInt();
				break;
			case 0:
				doSomething = false;
				break;
		}
		if (doSomething)
		{
			Employee employee = this.controller.getEmployee(id);
			if(employee != null)
			{
				return employee;
			}
			else
			{
				Console.print("Der kunne ikke findes en ansat med det valgte ID.");
				throw new Exception();
			}
		}
		else
		{
			return null;
		}
	}
	
	private int showList(ArrayList<Employee> employees)
	{
		int i = 0;
		for (Employee employee : employees)
		{
			i++;
			Console.print("{"+employee.getId()+"}"+employee.getName()+"\t"+employee.getLocation());
		}
		return i;
	}

	private void showInfo(Employee employee)
	{
		String location = (employee.getLocation() == Location.HardwareStore)?"Byggemarked":"Trælasthandel";
		Console.print("-Profil-");
		Console.print("ID: "+employee.getId());
		Console.print("Navn: "+employee.getName());
		Console.print("Lokation: "+employee.getLocation());
	}
}